package com.dyit.controller;

import com.dyit.dto.HttpResp;
import com.dyit.entity.Admin;
import com.dyit.entity.Book;
import com.dyit.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "管理员进行操作")
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    private IAdminService ias;
    @Autowired
    private RedisTemplate<String,Object> template;

    /**
     * 管理员登陆
     * @param account
     * @param password
     * @return
     */
    @ApiOperation("管理员登陆")
    @GetMapping("/login")
    public HttpResp login(String account,String password){
        Admin admin = ias.login(account, password);
        template.opsForValue().set("currentuser",admin);//把对象放到缓存里面去
        template.opsForValue().set("currentuserper",admin.getPer());//把对象的权限也放进去
        return new HttpResp(20001,"登陆成功！",admin.getPer(),null);
    }

    /**
     * 管理员添加书
     * @param book
     * @return
     */
    @ApiOperation("管理员添加书")
    @PutMapping("/addBook")
    public HttpResp addBook(Book book){
        ias.addBook(book);
        return new HttpResp(20002,"添加书success!",book,null);
    }

}
